#encoding: utf-8
class Bl < ActiveRecord::Base
  extend Opal::Enum
  attr_enum :status, BlStatusEnum

  belongs_to :user
  belongs_to :cy
  belongs_to :forward
  belongs_to :motorcade
  belongs_to :shipowner
  belongs_to :port

  has_many :bl_containers

  #根据复合条件查询，将来用于复用
  def self.query_bls(condition_map, page)
    Bl.where(condition_map).order("created_at DESC").paginate(:page => page, :per_page => Constant::PER_PAGE_4)
  end

  #查找在途箱
  #状态为out_cy,centralize_on_port,send_car
  def self.query_ctn_on_way_bls(condition_map, page)
    Bl.joins(:bl_containers).where(condition_map).
        where("bl_containers.status in( #{ContainerStatusEnum.get_enum(:out_cy).value}, #{ContainerStatusEnum.get_enum(:centralize_on_port).value} , #{ContainerStatusEnum.get_enum(:send_car).value})").order("created_at DESC").group('bl_no').paginate(:page => page, :per_page => Constant::PER_PAGE_4)
  end


  # 根据集装箱号返回给提单的集装箱对象
  # [ctn_no] 集装箱号，string
  # return:: ctn,提单集装箱对象,如果没有此箱号的集装箱，则返回nil
  #
  #
  # container = bl.get_bl_ctn("bl_ctn_001")
  def get_bl_ctn(ctn_no)
    self.bl_containers.find_by_ctn_no ctn_no
  end

  #新增集装箱
  #[ctns] 集装箱对象数组
  def add_ctn(ctns)
    logger.debug ctns
    ctns.each do |ctn|
      if self.bl_containers.find_by_ctn_no(ctn['ctn_no'])
        logger.debug "the ctn of ctn_no:#{ctn['ctn_no']}  already exists."
      else
        bl_ctn = self.bl_containers.build
        bl_ctn.ctn_no = ctn['ctn_no']
        bl_ctn.status = ctn['status'] || BlContainer::SEND_CAR
        bl_ctn.ctn_type = ctn['ctn_type']
        bl_ctn.seal_no = ctn['seal_no']
        bl_ctn.vehicle_no = ctn['vehicle_no']
        bl_ctn.bl_id = self.id
        bl_ctn.save!
      end
    end

  end

  #配箱
  #[ctn_type] 集装箱类型
  #[ctn_no] 集装箱号
  def bind_ctn(ctn_type, ctn_no)
    self.bl_containers.each do |ctn|
      if ctn.ctn_type == ctn_type and (ctn.ctn_no.nil? or ctn.ctn_no == "")
        ctn.ctn_no = ctn_no
        ctn.save
        return true
      else
        logger.error "没有这个类型的集装箱"
      end
    end
  end

  #修改集装箱
  #[old_ctn_no] 原来的集装箱号
  #[ctn] 新集装箱对象
  def modify_ctn(old_ctn_no, ctn)
    bl_ctn = self.bl_containers.where(:ctn_no => old_ctn_no).first
    if bl_ctn
      bl_ctn.ctn_no = ctn["ctn_no"]
      bl_ctn.status = ctn["status"]
      bl_ctn.ctn_type = ctn["ctn_type"]
      bl_ctn.seal_no = ctn["seal_no"]
      bl_ctn.vehicle_no = ctn["vehicle_no"]
      bl_ctn.save
    end
  end

  #部分撤仓
  #[ctn_no] 集装箱号
  def remove_part_ctn(ctn_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.delete
      logger.debug "the container with ctn_no:'#{ctn_no}' delete successfully."
    else
      logger.debug "there is no such container with ctn_no:'#{ctn_no}'."
    end
  end

  #出场
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def out_cy(ctn_no, vehicle_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.status_enum = :out_cy
      bl_ctn.vehicle_no = vehicle_no
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"出场", :message=>"出场",
                    :bl_container=>bl_ctn, :vehicle_no=> vehicle_no)
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #入场
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def in_cy(ctn_no, vehicle_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no, :vehicle_no => vehicle_no).first
    if bl_ctn
      bl_ctn.status_enum = :in_cy
      bl_ctn.vehicle_no = "NULL"
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"入场", :message=>"入场",
                    :bl_container=>bl_ctn, :vehicle_no=> vehicle_no)
      logger.debug "success"
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #运抵客户
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def deliver_to_client(ctn_no, vehicle_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no, :vehicle_no => vehicle_no).first
    if bl_ctn
      bl_ctn.status_enum = :deliver_to_client
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"运抵客户", :message=>"运抵客户",
                    :bl_container=>bl_ctn, :vehicle_no=> vehicle_no)
    else
      logger.error "no such ctn_no:'#{ctn_no}' with car_no:'#{vehicle_no}'."
    end
  end

  #集港
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def centralize_on_port(ctn_no, vehicle_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.status_enum = :centralize_on_port
      bl_ctn.vehicle_no = vehicle_no
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"集港", :message=>"集港",
                    :bl_container=>bl_ctn, :vehicle_no=> vehicle_no)
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #到港
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def export_arrive_port(ctn_no, vehicle_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no, :vehicle_no => vehicle_no).first
    if bl_ctn
      bl_ctn.status_enum = :export_arrive_port
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"到港", :message=>"到港",
                    :bl_container=>bl_ctn, :vehicle_no=> vehicle_no)
    else
      logger.error "no such ctn_no:'#{ctn_no}' with car_no:'#{vehicle_no}'."
    end
  end

  #查验
  #[ctn_no] 集装箱号
  def check(ctn_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.status_enum = :check
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"查验", :message=>"查验",
                    :bl_container=>bl_ctn)
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #放行
  #[ctn_no] 集装箱号
  def release(ctn_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.status_enum = :release
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"放行", :message=>"放行",
                    :bl_container=>bl_ctn)
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #运抵场站
  #[ctn_no] 集装箱号
  def arrive_cy(ctn_no)
    bl_ctn = self.bl_containers.where(:ctn_no => ctn_no).first
    if bl_ctn
      bl_ctn.status_enum = :arrive_cy
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"运抵场站", :message=>"运抵场站",
                    :bl_container=>bl_ctn)
    else
      logger.error "no such ctn_no:'#{ctn_no}'"
    end
  end

  #上船
  def aboard
    self.status_enum = :aboard
    self.save
    BlHistory.create(time: Time.now, action: "上船", message: "上船", bl: self)
  end

  #开船
  def sail
    self.status_enum = :sail
    self.save
    BlHistory.create(time: Time.now, action: "开船", message: "开船", bl: self)
  end


  #进口抵港
  def import_arrive_port
    self.status_enum = :importArrivePort
    self.save
    BlHistory.create(time: Time.now, action: "上船", message: "上船", bl: self)
  end

  #派车
  #[ctn_no] 集装箱号
  #[car_no] 车号
  def send_car(ctn_no, car_no)
    self.bl_containers.where(:ctn_no => ctn_no).each do |bl_ctn|
      bl_ctn.status_enum = :send_car
      bl_ctn.vehicle_no = car_no
      bl_ctn.save
      BlContainerHistory.create(:time=>Time.now, :action=>"派车", :message=>"派车",
                    :bl_container=>bl_ctn, :vehicle_no=> car_no)
    end
  end

end
